package com.sholmes.android.scribbler;

import java.util.Observable;

public class SensorProcessor extends Observable {
	private Integer ROTATION_CORRECTION = 45;
	private Integer _speed;
	private Integer _rotation;
	
	private Integer _previousSpeed;
	private Integer _previousRotation;
	
	
	public SensorProcessor(){
		this._previousSpeed=0;
		this._previousRotation=0;
		this._speed = 0;
	}

	public void setValues(float[] values){
		Float speed = values[1]+ROTATION_CORRECTION;		//Add 45degrees due to the position the device is held.
		Float rotation = values[2];

		//Required to remove outliers from calculations
		if(speed.intValue() == ROTATION_CORRECTION || speed.intValue() == 0){
//			this._speed = this._previousSpeed;
			setSpeed(this._previousSpeed);
		}else{
			this._previousSpeed = speed.intValue();
			setSpeed(speed.intValue());
		}
		
		if(rotation.intValue() == 0 || rotation.intValue() == 90){
//			this._rotation = this._previousRotation;
			setRotation(this._previousRotation);
		}else{
			this._previousRotation = rotation.intValue();
			setRotation(rotation.intValue());
		}
		
//		this.setRotation(rotation.intValue());
//		this.setSpeed(speed.intValue());
		
		//Sets flag to indicate this observable object has been updated
		this.setChanged();
		
		//Triggers all registered observers to call update() method
		this.notifyObservers();
	}

	public Integer getSpeed() {
		return _speed;		
	}


	private void setSpeed(Integer speed) {
		_speed = speed;		
	}


	public Integer getRotation() {
		return _rotation;		
	}


	private void setRotation(Integer direction) {
		_rotation = direction;
	}

	/**
	 * Return a array of valid stop values.
	 * @return values of the robot when it is stopped.
	 */
	public float[] getStoppedValue() {
		return new float[] {0,-ROTATION_CORRECTION,0};
	}
	
	
	
}
